/*
 * ProtocolSender.cpp
 *
 *  Created on: Sep 8, 2017
 *      Author: guoxs
 */

#include "uart/ProtocolSender.h"
#include "uart/UartContext.h"
#include "utils/Log.h"
#include <stdio.h>

extern BYTE getCheckSum(const BYTE *pData, int len);

/**
 * 需要根据协议格式进行拼接，以下只是个模板
 */
bool sendProtocol(const UINT16 cmdID, const BYTE *pData, BYTE len) {
	if (len + DATA_PACKAGE_MIN_LEN > 256) {
		LOGE("sendProtocol data is too len !!!\n");
		return false;
	}

	BYTE dataBuf[256];

	dataBuf[0] = CMD_HEAD1;
	dataBuf[1] = CMD_HEAD2;			// 同步帧头

	dataBuf[2] = HIBYTE(cmdID);
	dataBuf[3] = LOBYTE(cmdID);		// 命令字节

	dataBuf[4] = len;

	UINT frameLen = 5;

	// 数据
	for (int i = 0; i < len; ++i) {
		dataBuf[frameLen] = pData[i];
		frameLen++;
	}

#ifdef PRO_SUPPORT_CHECK_SUM
	// 校验码
	dataBuf[frameLen] = getCheckSum(dataBuf, frameLen);
	frameLen++;
#endif

	return UARTCONTEXT->send(dataBuf, frameLen);
}
void yh_lamp_on(BYTE id){
	BYTE dataBuf[256];
	dataBuf[0] = 0x55;
	dataBuf[1] = 0x41; // lamp cmd
	dataBuf[2] = 0x0B; //length
	dataBuf[3] = id;
	dataBuf[4] = 0x60;
	dataBuf[5] = 0xAA;
	dataBuf[6] = 0x55;
	dataBuf[7] = 0xAA;
	dataBuf[8] = 0x55;
	dataBuf[9] = 0xAA;
	dataBuf[10] = 0x55;
	dataBuf[11] = 0xAA;
	dataBuf[12] = 0x55;
	dataBuf[13] = 0xAA;
	dataBuf[14] = 0xAA;
	UARTCONTEXT->send(dataBuf, dataBuf[2]+4);
}
void yh_lamp_off(BYTE id){
	BYTE dataBuf[256];
	dataBuf[0] = 0x55;
	dataBuf[1] = 0x41; // lamp cmd
	dataBuf[2] = 0x0B; //length
	dataBuf[3] = id;
	dataBuf[4] = 0x61;
	dataBuf[5] = 0xAA;
	dataBuf[6] = 0x55;
	dataBuf[7] = 0xAA;
	dataBuf[8] = 0x55;
	dataBuf[9] = 0xAA;
	dataBuf[10] = 0x55;
	dataBuf[11] = 0xAA;
	dataBuf[12] = 0x55;
	dataBuf[13] = 0xAA;
	dataBuf[14] = 0xAA;
	UARTCONTEXT->send(dataBuf, dataBuf[2]+4);
}
void yh_lamp_temp(BYTE id,BYTE light,BYTE temp){
	BYTE dataBuf[256];
	dataBuf[0] = 0x55;
	dataBuf[1] = 0x41; // lamp cmd
	dataBuf[2] = 0x0B; //length
	dataBuf[3] = id;
	dataBuf[4] = 0x01;
	dataBuf[5] = light;
	dataBuf[6] = temp;
	dataBuf[7] = 0x0A;
	dataBuf[8] = 0x0A;
	dataBuf[9] = 0xAA;
	dataBuf[10] = 0x55;
	dataBuf[11] = 0xAA;
	dataBuf[12] = 0x55;
	dataBuf[13] = 0xAA;
	dataBuf[14] = 0xAA;
	UARTCONTEXT->send(dataBuf, dataBuf[2]+4);
}
void yh_sence(BYTE sence){
	BYTE dataBuf[256];
	dataBuf[0] = 0x55;
	dataBuf[1] = 0x44; // sence cmd
	dataBuf[2] = 0x01; //length
	dataBuf[3] = sence;
	dataBuf[4] = 0xaa;
	UARTCONTEXT->send(dataBuf, 5);
}


void yh_window_open(BYTE id){
	BYTE dataBuf[256];
	dataBuf[0] = 0x55;
	dataBuf[1] = 0x43; // lamp cmd
	dataBuf[2] = 0x02; //length
	dataBuf[3] = id;
	dataBuf[4] = 0x01;
	UARTCONTEXT->send(dataBuf, dataBuf[2]+4);
}

void yh_window_close(BYTE id){
	BYTE dataBuf[256];
	dataBuf[0] = 0x55;
	dataBuf[1] = 0x43; // lamp cmd
	dataBuf[2] = 0x02; //length
	dataBuf[3] = id;
	dataBuf[4] = 0x02;
	UARTCONTEXT->send(dataBuf, dataBuf[2]+4);
}
void yh_window_stop(BYTE id){
	BYTE dataBuf[256];
	dataBuf[0] = 0x55;
	dataBuf[1] = 0x43; // lamp cmd
	dataBuf[2] = 0x02; //length
	dataBuf[3] = id;
	dataBuf[4] = 0x03;
	UARTCONTEXT->send(dataBuf, dataBuf[2]+4);
}
void yh_window_value(BYTE id,BYTE open){
	BYTE dataBuf[256];
	dataBuf[0] = 0x55;
	dataBuf[1] = 0x43; // lamp cmd
	dataBuf[2] = 0x03; //length
	dataBuf[3] = id;
	dataBuf[4] = 0x04;
	dataBuf[5] = open;
	UARTCONTEXT->send(dataBuf, dataBuf[2]+4);
}

void yh_air_open(BYTE id){
	BYTE dataBuf[256];
	dataBuf[0] = 0x55;
	dataBuf[1] = 0x42; // lamp cmd
	dataBuf[2] = 0x04; //length
	dataBuf[3] = id;
	dataBuf[4] = 0x01;
	dataBuf[5] = 0x08;
	dataBuf[6] = 0;
	UARTCONTEXT->send(dataBuf, dataBuf[2]+4);
}
void yh_air_close(BYTE id){
	BYTE dataBuf[256];
	dataBuf[0] = 0x55;
	dataBuf[1] = 0x42; // lamp cmd
	dataBuf[2] = 0x04; //length
	dataBuf[3] = id;
	dataBuf[4] = 0x01;
	dataBuf[5] = 0x00;
	dataBuf[6] = 0;
	UARTCONTEXT->send(dataBuf, dataBuf[2]+4);
}
void yh_air_settemp(BYTE id,BYTE temp){
	BYTE dataBuf[256];
	dataBuf[0] = 0x55;
	dataBuf[1] = 0x42; // lamp cmd
	dataBuf[2] = 0x04; //length
	dataBuf[3] = id;
	dataBuf[4] = 0x04;
	dataBuf[5] = ((temp-16)&0x0f << 4 )|0x09;
	dataBuf[6] = 0;
	UARTCONTEXT->send(dataBuf, dataBuf[2]+4);
}
void yh_air_setmode(BYTE id,BYTE mode){
	BYTE dataBuf[256];
	dataBuf[0] = 0x55;
	dataBuf[1] = 0x42; // lamp cmd
	dataBuf[2] = 0x04; //length
	dataBuf[3] = id;
	dataBuf[4] = 0x02;
	dataBuf[5] = mode|0x08;
	dataBuf[6] = 0;
	UARTCONTEXT->send(dataBuf, dataBuf[2]+4);
}
